package com.tencent.pet.service.impl;

import com.tencent.basic.exception.BusinessException;
import com.tencent.basic.service.IBaiduAiAuditService;
import com.tencent.basic.service.impl.BaseServiceImpl;
import com.tencent.basic.util.LoginContext;
import com.tencent.org.domain.Employee;
import com.tencent.org.mapper.EmployeeMapper;
import com.tencent.pet.domain.Pet;
import com.tencent.pet.domain.PetDetail;
import com.tencent.pet.domain.PetOnlineAuditLog;
import com.tencent.pet.mapper.PetDetailMapper;
import com.tencent.pet.mapper.PetMapper;
import com.tencent.pet.mapper.PetOnlineAuditLogMapper;
import com.tencent.pet.service.IPetService;
import com.tencent.user.domain.Logininfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * 业务实现类：
 */
@Service
public class PetServiceImpl extends BaseServiceImpl<Pet> implements IPetService {

    @Autowired
    private PetOnlineAuditLogMapper petOnlineAuditLogMapper;

    @Autowired
    private IBaiduAiAuditService baiduAiAuditService;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private PetMapper petMapper;

    @Autowired
    private PetDetailMapper petDetailMapper;

    @Override
    @Transactional
    public void save(Pet pet) {
        super.save(pet);

        // 宠物明细
        PetDetail petDetail = pet.getPetDetail();

        if (petDetail != null) { // 如果有数据
            petDetail.setPetId(pet.getId()); // 设置当前宠物的id进明细表
            petDetailMapper.save(petDetail); // 添加
        }
    }

    @Override
    @Transactional
    public void update(Pet pet) {
        super.update(pet);

        // 宠物明细
        PetDetail petDetail = pet.getPetDetail();

        if (petDetail != null) { // 如果有数据
            petDetail.setPetId(pet.getId()); // 设置当前宠物的id进明细表
            petDetailMapper.updatePetDetailByPetId(petDetail); // 修改
        }
    }

    @Override
    @Transactional
    public void remove(Long id) {
        super.remove(id);

        petDetailMapper.delPetDetailByPetId(id);
    }

    /**
     * 重写删除方法 因为批量删除的时候也要将明细批量删除
     * @param ids
     */
    @Override
    @Transactional
    public void patchDelete(Long[] ids) {
        for (Long id : ids) {
            remove(id);
        }
    }

    /**
     * 批量下架
     * @param ids
     */
    @Override
    @Transactional
    public void offsale(Long[] ids) {
        // 下架 要修改下架时间
        Date offsaletime = new Date();
        // 设置状态 - 在sql写死 =0
        // sql循环ids修改
        petMapper.patchOffsale(offsaletime, ids);
    }

    /**
     * 批量上架
     * @param ids
     * @param request - 获取登录人信息的 用来做审核人
     */
    @Override
    @Transactional
    public void onsale(Long[] ids, HttpServletRequest request) {
        Logininfo logininfo = LoginContext.getLoginUser(request);
        // 通过Longininfo登录人的id 查到 employee对象
        Employee employee = employeeMapper.loadByLogininfoId(logininfo.getId());

        for (Long id : ids) {
            // 当前遍历到的pet对象
            Pet pet = petMapper.loadById(id);
            // 审核名字 和 resources 是否合规 - 百度智能审核自研
            Boolean textAudit = baiduAiAuditService.textAudit(pet.getName());
            Boolean imageAudit = baiduAiAuditService.imageAudit(pet.getResources());

            if (textAudit && imageAudit) {
                // 审核通过 - 修改上架时间 修改状态为上架
                pet.setOnsaletime(new Date());
                pet.setState(1);
                petMapper.update(pet);

                // 获取一个新的审核日志对象 - 添加审核日志
                PetOnlineAuditLog petLog = new PetOnlineAuditLog();
                petLog.setPetId(pet.getId());
                // 1为审核通过 0是待审核 -1 是驳回
                petLog.setState(1);
                petLog.setAuditTime(new Date());
                petLog.setNote("审核成功");
                // 审核人id
                petLog.setAuditId(employee.getId());

                petOnlineAuditLogMapper.save(petLog);

            } else {
                // 审核失败 - 添加审核日志
                PetOnlineAuditLog petLog = new PetOnlineAuditLog();
                petLog.setPetId(pet.getId());
                // 1为审核通过 0是待审核 -1 是驳回
                petLog.setState(-1);
                petLog.setAuditTime(new Date());
                petLog.setNote("审核失败咯,内容有非法数据！");
                // 审核人id
                petLog.setAuditId(employee.getId());
                petOnlineAuditLogMapper.save(petLog);

                throw new BusinessException("审核失败咯,内容有非法数据！");
            }
        }
    }
}
